//
//  Tools.swift
//  BWPanelKit
//
//  Created by zhuxuhong on 2022/3/8.
//

import Foundation

extension UIImage {
    static func render(
        path: UIBezierPath,
        iconSize: CGSize,
        imageSize: CGSize,
        lineWidth: CGFloat,
        align position: CGPoint? = nil
    ) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
        guard let ctx = UIGraphicsGetCurrentContext() else {
            return .init()
        }
        let outerLayer = CAShapeLayer()
        let shapeLayer = CAShapeLayer()
        
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = HUD.shared.appearance.tintColor.cgColor
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineCap = .round
        shapeLayer.lineJoin = .round
        
        outerLayer.addSublayer(shapeLayer)
        if let p = position {
            shapeLayer.position = p
        }
        else {
            shapeLayer.position = .init(x: imageSize.width/2-iconSize.width/2, y: imageSize.height/2-iconSize.height/2)
        }
        outerLayer.render(in: ctx)
        
        let image = UIGraphicsGetImageFromCurrentImageContext() ?? .init()
        UIGraphicsEndImageContext()
        return image
    }
    
    static func excmark(iconSize: CGSize, imageSize: CGSize, lineWidth: CGFloat) -> UIImage {
        let path = UIBezierPath()
        let halfW = lineWidth/2
        let midX = iconSize.width/2-halfW
        path.move(to: .init(x: midX, y: halfW))
        path.addLine(to: .init(x: midX, y: iconSize.height-lineWidth*3-halfW))
        path.move(to: .init(x: midX, y: iconSize.height-lineWidth))
        path.addLine(to: .init(x: midX, y: iconSize.height-halfW))
        return render(path: path, iconSize: iconSize, imageSize: imageSize, lineWidth: lineWidth)
    }
    
    static func xmark(iconSize: CGSize, imageSize: CGSize, lineWidth: CGFloat) -> UIImage {
        let path = UIBezierPath()
        let halfW = lineWidth/2
        path.move(to: .init(x: halfW, y: halfW))
        path.addLine(to: .init(x: iconSize.width-halfW, y: iconSize.height-halfW))
        path.move(to: .init(x: iconSize.width-halfW, y: halfW))
        path.addLine(to: .init(x: halfW, y: iconSize.height-halfW))
        return render(path: path, iconSize: iconSize, imageSize: imageSize, lineWidth: lineWidth)
    }
    
    static func checkmark(iconSize: CGSize, imageSize: CGSize, lineWidth: CGFloat) -> UIImage {
        let path = UIBezierPath()
        let halfL = lineWidth/2
        let halfW = iconSize.width/2
        let halfH = iconSize.height/2
        path.move(to: .init(x: halfW/2+halfL, y: halfH-halfL))
        path.addLine(to: .init(x: halfW-halfL, y: iconSize.height-halfL))
        path.move(to: .init(x: halfW-halfL, y: iconSize.height-halfL))
        path.addLine(to: .init(x: iconSize.width-halfL, y: halfL))
        let position = CGPoint(x: -(halfW/2+halfL)/2, y: imageSize.height/2-iconSize.height/2)
        return render(path: path, iconSize: iconSize, imageSize: imageSize, lineWidth: lineWidth, align: position)
    }
}

